﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-5-11 13:14:22
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL.TaoBao;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL.TaoBao
{
    /// <summary>
    /// 数据访问类 TB_SHANGDIAN
    /// </summary>
    public class TB_SHANGDIAN : ITB_SHANGDIAN
    {
        public TB_SHANGDIAN()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.TaoBao.TB_SHANGDIAN model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@KH_ID", SqlDbType.Int),
                    new SqlParameter("@TBZH", SqlDbType.VarChar,20),
                    new SqlParameter("@TBNC", SqlDbType.VarChar,100),
                    new SqlParameter("@PWD", SqlDbType.VarChar,50),
                    new SqlParameter("@BZ", SqlDbType.VarChar,255),
                    new SqlParameter("@DDLY", SqlDbType.VarChar,20),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;
            parameters[3].Value = model.KH_ID;

            if (model.TBZH != null)
                parameters[4].Value = model.TBZH;
            else
                parameters[4].Value = DBNull.Value;


            if (model.TBNC != null)
                parameters[5].Value = model.TBNC;
            else
                parameters[5].Value = DBNull.Value;


            if (model.PWD != null)
                parameters[6].Value = model.PWD;
            else
                parameters[6].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[7].Value = model.BZ;
            else
                parameters[7].Value = DBNull.Value;


            if (model.DDLY != null)
                parameters[8].Value = model.DDLY;
            else
                parameters[8].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[9].Value = model.field1;
            else
                parameters[9].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[10].Value = model.field2;
            else
                parameters[10].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_ADD", parameters, out rowsAffected);

            if (rowsAffected > 0)
                return (int)parameters[0].Value;
            else
                return 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaoBao.TB_SHANGDIAN model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@KH_ID", SqlDbType.Int),
                    new SqlParameter("@TBZH", SqlDbType.VarChar,20),
                    new SqlParameter("@TBNC", SqlDbType.VarChar,100),
                    new SqlParameter("@PWD", SqlDbType.VarChar,50),
                    new SqlParameter("@BZ", SqlDbType.VarChar,255),
                    new SqlParameter("@DDLY", SqlDbType.VarChar,20),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Value = model.MID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;
            parameters[3].Value = model.KH_ID;

            if (model.TBZH != null)
                parameters[4].Value = model.TBZH;
            else
                parameters[4].Value = DBNull.Value;


            if (model.TBNC != null)
                parameters[5].Value = model.TBNC;
            else
                parameters[5].Value = DBNull.Value;


            if (model.PWD != null)
                parameters[6].Value = model.PWD;
            else
                parameters[6].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[7].Value = model.BZ;
            else
                parameters[7].Value = DBNull.Value;


            if (model.DDLY != null)
                parameters[8].Value = model.DDLY;
            else
                parameters[8].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[9].Value = model.field1;
            else
                parameters[9].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[10].Value = model.field2;
            else
                parameters[10].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            int rowsAffected;
            SqlParameter[] parameters ={ };
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_GetMaxId", parameters, out rowsAffected);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaoBao.TB_SHANGDIAN GetModel(int MID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_SHANGDIAN> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_SHANGDIAN_GetList", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_SHANGDIAN> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[TB_SHANGDIAN]", null, pageSize, pageIndex, fldSort, sort, strCondition, "MID", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TaoBao.TB_SHANGDIAN GetModel(DataRow r)
        {
            Model.TaoBao.TB_SHANGDIAN model = new Model.TaoBao.TB_SHANGDIAN();
            model.MID = SqlHelper.GetInt(r["MID"]);
            model.PID = SqlHelper.GetInt(r["PID"]);
            model.TN_ID = SqlHelper.GetInt(r["TN_ID"]);
            model.KH_ID = SqlHelper.GetInt(r["KH_ID"]);
            model.TBZH = SqlHelper.GetString(r["TBZH"]);
            model.TBNC = SqlHelper.GetString(r["TBNC"]);
            model.PWD = SqlHelper.GetString(r["PWD"]);
            model.BZ = SqlHelper.GetString(r["BZ"]);
            model.DDLY = SqlHelper.GetString(r["DDLY"]);
            model.field1 = SqlHelper.GetString(r["field1"]);
            model.field2 = SqlHelper.GetString(r["field2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.TaoBao.TB_SHANGDIAN> GetList(DataSet ds)
        {
            List<Model.TaoBao.TB_SHANGDIAN> l = new List<Model.TaoBao.TB_SHANGDIAN>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
